from django.db.models import Sum, Avg
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt
from rest_framework import status, permissions
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework.views import APIView

from apps.blog.models import Collect, province
from apps.blog.serializers import CollectSerializer
from apps.message_form.models import Message


from apps.message_form.serializers import MessageSerializer, TrendSerializer, HotSerializer, SellerSerializer, \
    StockSerializer, RankSerializer, MapSerializer


def message_form(request):
    # 从html中提取数据保存到数据库中
    # get 拉，POST 推
    message = Message()
    if request.method == "POST":
        name = request.POST.get("name", "")
        type = request.POST.get("type", "")
        value = request.POST.get("value", "")
        img_url = request.POST.get("img_url", "")
        province = request.POST.get("province", "")
        city = request.POST.get("city", "")
        lng = request.POST.get("lng", "")
        lat = request.POST.get("lat", "")
        location = request.POST.get("sever_add", "")
        content = request.POST.get("message", "")

        message.name = name
        message.type = type
        message.value = value
        message.img_url = img_url
        message.province = province
        message.city = city
        message.lng = lng
        message.lat = lat
        message.location = location
        message.content = content
        message.save()
        return render(request, "/collect", {
            "message": message
        })


    if request.method == "GET":
        var_dict = {}
        all_message = Message.objects.all()
        if all_message:
            message = all_message[0]
            var_dict = {
                "message": message
            }
        return render(request, "message_form.html",var_dict)


class MessageList(APIView):

    @csrf_exempt
    def get(self,request):
        queryset = Collect.objects.all()
        s = MessageSerializer(instance = queryset,many=True)
        return Response(s.data,status=status.HTTP_200_OK)

    # def post(self,request):
    #     s = MessageSerializer(data=request.data)
    #     if s.is_valid():
    #         s.save()
    #         print(type(request.data),type(s.data))
    #         return Response(data=s.data,status=status.HTTP_201_CREATED)
    #     return Response(s.errors,status=status.HTTP_400_BAD_REQUEST)


class TrendList(APIView):
    @csrf_exempt
    def get(self,request):
        queryset = Message.objects.all()
        count = Message.objects.values('name').annotate(average_rating=Avg(''))
        s = TrendSerializer(instance = queryset,many=True)
        return Response(s.data,status=status.HTTP_200_OK)


class MapList(APIView):
    @csrf_exempt
    def get(self,request):
        queryset = Collect.objects.all()
        s = MapSerializer(instance = queryset,many=True)
        return Response(s.data,status=status.HTTP_200_OK)


class HotList(APIView):
    @csrf_exempt
    def get(self,request):
        queryset = Message.objects.all()
        s = HotSerializer(instance = queryset,many=True)
        return Response(s.data,status=status.HTTP_200_OK)


class SellerList(APIView):
    @csrf_exempt
    def get(self,request):
        queryset = Collect.objects.all()
        s = SellerSerializer(instance = queryset,many=True)
        return Response(s.data,status=status.HTTP_200_OK)


class StockList(APIView):
    @csrf_exempt
    def get(self,request):
        queryset = Collect.objects.all()
        s = StockSerializer(instance = queryset,many=True)
        return Response(s.data,status=status.HTTP_200_OK)


class RankList(APIView):
    @csrf_exempt
    def get(self, request):
        provinces = province.objects.all()
        s = RankSerializer(instance=provinces, many=True)
        return Response(s.data, status=status.HTTP_200_OK)